/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package give.mapreplay;

import give.replay.ReplayWindow;
import de.saar.penguin.give2.mapviewer.ControllerArrowsPlugin;
import de.saar.penguin.give2.mapviewer.JMapViewer;
import de.saar.penguin.give2.mapviewer.MapViewerTooltipOverlay;
import de.saar.penguin.give2.mapviewer.UpdateNotificationPlugin;
import give.comm.ActionMessage;
import give.comm.ClientMetadata;
import give.comm.DidNotUnderstandMessage;
import give.comm.Message;
import give.comm.StatusMessage;
import give.comm.TextMessage;
import give.comm.handling.MessageHandlingException;
import give.formula.Atom;
import give.formula.Formula;
import give.world.World;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

/**
 *
 * @author koller
 */
public class MapReplayWindow implements ReplayWindow {
    private JMapViewer mapViewer;
    private UpdateNotificationPlugin notificationPlugin;
    private TextNotificationPlugin textPlugin;
    private JFrame mapViewerFrame;

    @Override
    // world must be clone of originalWorld
    public void openReplayWindow(World world, String windowTitle, ClientMetadata clientMetadata) {
        if (isReplayWindowOpen()) {
            closeReplayWindow();
        }

        // set up map frame
        mapViewerFrame = new JFrame(windowTitle);
        mapViewerFrame.setLocationByPlatform(true);
        mapViewerFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

        mapViewer = new JMapViewer(world);
        mapViewer.setShowAxisLabels(true);
        mapViewer.setAlignWithPlayer(false);

        ControllerArrowsPlugin controllerArrows = new ControllerArrowsPlugin(world, mapViewer);
        mapViewer.addMouseOverObjectListener(controllerArrows);
        mapViewer.addMapAnnotationPlugin(controllerArrows);

        MapViewerTooltipOverlay mgp = new MapViewerTooltipOverlay(world, mapViewer);
        mapViewer.addMouseOverObjectListener(mgp);
        mapViewer.addMapAnnotationPlugin(mgp);

        notificationPlugin = new UpdateNotificationPlugin(mapViewer);
        mapViewer.addMapAnnotationPlugin(notificationPlugin);

        textPlugin = new TextNotificationPlugin(mapViewer);
        mapViewer.addMapAnnotationPlugin(textPlugin);

        mapViewerFrame.add(mapViewer);

        mapViewerFrame.pack();
        mapViewerFrame.setVisible(true);
        mapViewerFrame.setResizable(false);

    }

    private void closeReplayWindow() {
        if (isReplayWindowOpen()) {
            mapViewerFrame.setVisible(false);

            mapViewerFrame = null;
            mapViewer = null;
            notificationPlugin = null;
            textPlugin = null;
        }
    }

    public void handleException(MessageHandlingException e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void handleStatusMessage(StatusMessage statusMessage) throws MessageHandlingException {
        mapViewer.repaint();
    }

    public void handleActionMessage(ActionMessage actionMessage, List<Formula> formulas) throws MessageHandlingException {
        for (Formula f : formulas) {
            if (f instanceof Atom) {
                Atom a = (Atom) f;

                if (a.getPredicate().equals("state")) {
                    notificationPlugin.addUpdateAnnotation(a.getArgumentAsString(0));
                }
            }
        }

        mapViewer.repaint();
    }

    public void handleMessage(Message message) throws MessageHandlingException {
        if (message instanceof TextMessage) {
            String text = ((TextMessage) message).getText();
            textPlugin.addTextAnnotation(text);
        } else if (message instanceof DidNotUnderstandMessage) {
            notificationPlugin.addHelpAnnotation();
        }
    }

    private boolean isReplayWindowOpen() {
        return mapViewerFrame != null;
    }

    @Override
    public void synchronizeWorld() {
        
    }
}
